<template>
  <el-drawer
    :visible.sync="drawer"
    direction="rtl"
    size="50%"
    :with-header="false"
    :width="700"
    :append-to-body="true"
  >
    <div class="drawer_box">
      <div class="drawer_title">
        <div class="drawer_t">
          <div class="drawer_t_l">
            <img
              class="drawer_t_l_img"
              src="/static/skin/herodoo/main/platform/home/img/piliangoumai.png"
              alt=""
            />
            添加推荐
          </div>
          <img
            class="drawer_t_r"
            src="/static/skin/herodoo/main/platform/home/img/guanbi.png"
            alt=""
            @click="drawer = false"
          />
        </div>
      </div>
      <div class="drawer_content">
        <div class="list-box">
          <div class="left">排序:</div>
          <div class="right">
            <el-input-number
              v-model="form.sort"
              @change="handleChange"
              :min="1"
              label="排序"
              :disabled="isEdit == 'look' ? true : false"
            ></el-input-number>
          </div>
        </div>
        <div class="list-box">
          <div class="left">主题类型:</div>
          <div class="right">{{ typeObj.Name }}</div>
        </div>
        <div class="list-box" v-if="form.selectArr.length > 0">
          <div class="left">已选自有权益:</div>
          <div class="right">
            <div
              style="display:flex;width: 100%;"
              v-for="(item, i) in form.selectArr"
              :key="item.id"
            >
              <div
                style="padding:1.3rem;background: #FFFFFF;box-shadow: 2px 1px 9px 0px rgba(180,171,206,0.25);border-radius: 18px 18px 18px 18px;width: 100%;margin-bottom: 1.3rem;"
              >
                <div class="list">
                  <div class="item">
                    <div style="width: 30%;height: 100%;margin-right: 2%;">
                      <img
                        style="width: 100%;height: 100%;border-radius: 8px;"
                        :src="JSON.parse(item.MainPicturePath)[0].url"
                        :alt="JSON.parse(item.MainPicturePath)[0].name"
                      />
                    </div>
                    <div
                      style="display: flex;flex-direction: column;justify-content: space-between;width:68%;"
                    >
                      <div>
                        <p
                          style="font-size:2rem;color:#1E1B19;line-height:3rem;font-weight: 600;"
                        >
                          {{ item.Name }}
                        </p>
                        <div>
                          <span
                            v-for="item1 in getTag(item.EquityFlagListName)"
                            style="padding:4px;border-radius: 2px 2px 2px 2px;border:1px solid #426CE9;display:inline-block;
                                                        font-size:1rem;line-height:1rem;color:#426CE9;margin-right: 0.4rem;"
                            >{{ item1 }}</span
                          >
                        </div>
                        <div style="color:#1E1B19;">
                          <span style="color: #EC5D31;padding-right: 10px;"
                            >单价：￥{{ item.SalePrice }}</span
                          >
                        </div>
                      </div>
                      <div
                        style="display: flex;justify-content: space-between;"
                      >
                        <div>
                          <span
                            style="font-size:1.2rem;color:#827070;margin-right:2rem;"
                            >库存：{{ item.InventoryQuantity }}份</span
                          >
                          <span
                            style="font-size:1.2rem;color:#827070;margin-right:2rem;"
                            >已售：{{ item.SoldQuantity }}份</span
                          >
                        </div>
                        <div
                          style="font-size: 2.5rem;font-weight: bold;color: #EC5D31;"
                        >
                          总价：￥{{ item.SalePrice.toFixed(2) }}
                        </div>
                      </div>
                    </div>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>
        <div class="list-box" v-if="isEdit == 'look' ? false : true">
          <div class="left">权益:</div>
          <div class="right">
            <div class="add_content" @click="openEquity">
              <i class="el-icon-plus"></i>
              <span>点击添加权益</span>
            </div>
          </div>
        </div>
        <div class="list-box">
          <div class="left">海报:</div>
          <div class="right">
            <div class="tips">
              海报可上传1张图片，图片不超过512KB，仅支持jpg、png格式
            </div>
            <imgUplode
              :list="form.fileList"
              @imgList="getImgsUrl"
              :octopus="octopus"
              num="1"
              :isEdit="isEdit == 'look' ? true : false"
            >
            </imgUplode>
          </div>
        </div>
        <div class="list-box" v-if="type == 'look'">
          <div class="left">操作人:</div>
          <div class="right">
            admin
          </div>
        </div>
        <div class="list-box" v-if="type == 'look'">
          <div class="left">操作时间:</div>
          <div class="right">
            2024-6-30
          </div>
        </div>
      </div>

      <div class="button_list" v-if="isEdit != 'look'">
        <button @click="save()">保存</button>
      </div>
    </div>
    <EquityList @getList="getList" ref="EquityList"></EquityList>
  </el-drawer>
</template>

<script>
import EquityList from "./equityList.vue";
import imgUplode from '@/components/Components/imgUpdate.vue'
export default {
  name: "cat_dialog",
  components: {
    EquityList,
    imgUplode
  },
  props: {
    // isEdit: {
    //   type: Boolean,
    //   default: true
    // }
  },
  data() {
    return {
      octopus: this.$parent.octopus,
      drawer: false,
      isEdit: true,
      type: "CREATE",
      currentPage: 1,
      form: {
        sort: 1, //排序
        fileList: [], //海报
        selectArr: [],
        ID: ""
      },
      rules: {
        selectArr: { required: true, message: "请选择一个权益" }
      },
      typeObj: {}
    };
  },
  created() {},
  watch: {},
  methods: {
    drawerOpen(data = null, type = "CREATE", typeObj) {
      this.drawer = true;
      this.isEdit = type;
      this.typeObj = typeObj;
      if (!data) {
        this.form.sort = 1;
        this.form.fileList = [];
        this.form.selectArr = [];
        this.form.ID = [];
        return;
      } else {
        this.form.sort = data.Sort;
        if(data.hasOwnProperty('PosterPath')){
          this.form.fileList = JSON.parse(data.PosterPath)
        }else{
          this.form.fileList = []
        }
        this.form.selectArr = [];
        this.form.ID = data.ID;
        this.getEquity(data.EquityID);
      }
    },
    getImgsUrl(fileList) {
      this.$set(this.form.fileList, 0, fileList[0]);
    },
    save() {
      if(this.form.selectArr.length == 0){
        this.octopus.tips.error("请选择一个权益。");
        return
      }
      this.$confirm(`确定保存这个主题推荐吗?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        // type: 'warning'
        center: true
      })
        .then(res => {
          this.update_api_1278();
        })
        .catch(() => {
          console.log("取消了");
        });
    },
    getList(list, currentPage) {
      this.currentPage = currentPage;
      this.form.selectArr = list;
    },
    update_api_1278() {
      let cellDate = [
        { name: "EquityID", defaultValue: this.form.selectArr[0].ID }, // 权益ID
        { name: "Sort", defaultValue: this.form.sort }, // 排序优先级
        { name: "SubjectID", defaultValue: this.typeObj.ID }, // 主题\推荐ID
        { name: "UpdateTime", show: false }, // 修改时间
        { name: "UpdateUserID", show: false }, // 修改人
        { name: "PosterPath", defaultValue: JSON.stringify(this.form.fileList) } // 海报
      ];
      if (this.isEdit == "CREATE") {
        cellDate.push({
          name: "CreateTime",
          defaultValue:
            new Date().toJSON().substring(0, 10) +
            " " +
            new Date().toTimeString().substring(0, 8)
        });
        cellDate.push({
          name: "CreateUserID",
          defaultValue: this.octopus.passport.id
        });
      } else {
        cellDate.push({
          name: "UpdateTime",
          defaultValue:
            new Date().toJSON().substring(0, 10) +
            " " +
            new Date().toTimeString().substring(0, 8)
        });
        cellDate.push({
          name: "UpdateUserID",
          defaultValue: this.octopus.passport.id
        });
      }
      this.octopus.request.api.post(
        "/Octopus/Engine/Item",
        {
          id: 1278,
          type: this.isEdit,
          value: this.isEdit == "CREATE" ? "" : this.form.ID,
          cellData: cellDate,
          paramData: {}
        },
        data => {
          if (data.status.code == 200) {
            this.drawer = false;
            this.octopus.tips.success("操作成功。");
            this.$emit("reload");
          } else {
            this.octopus.tips.error(data);
          }
        }
      );
    },
    // 打开自有权益弹窗
    openEquity() {
      this.$refs.EquityList.drawerOpen(
        this.isEdit,
        this.form.selectArr,
        this.currentPage
      );
    },
    getUrl(url) {
      let str = JSON.parse(url);
      return str[0].url;
    },
    getTag(str) {
      let arr = str.split(",");
      return arr;
    },
    // 获取自有权益
    getEquity(ID) {
      this.octopus.request.api.post(
        "/Octopus/Engine/List",
        {
          id: 1181,
          cellData: [
            { name: "CompanyID" }, // 权益所属公司
            { name: "CostPrice" }, // 平台价格
            { name: "CreateTime" }, // 权益创建时间
            { name: "EffectiveTime" }, // 生效时间
            { name: "EquityFlagList" }, // 权益标签
            { name: "EquityFlagListName" }, // 权益标签中文
            { name: "ExpireTime" }, // 过期时间
            { name: "ID", filterKey: "=", filterValue: ID }, // 权益ID
            { name: "InventoryQuantity" }, // 库存数量
            { name: "MainPicturePath" }, // 主图
            { name: "Name" }, // 权益名称
            { name: "ProductFlagList" }, // 产品标签
            { name: "ProductFlagListName" }, // 产品标签 中文
            { name: "SalePrice" }, // 市场价格
            { name: "SecondaryPicturePath" }, // 副图
            { name: "SoldQuantity" }, // 已售数量
            { name: "Status" }, // 权益状态
            { name: "TransferRuleID" }, // 采购公司限制
            { name: "TransferRuleIDName" }, // 采购公司限制中文
            { name: "TypeID" }, // 权益类型
            { name: "TypeIDName" } // 权益类型 （中文）
          ],
          paramData: {
            CompanyID:
              this.octopus.passport.authorization.company == 1
                ? "NULL"
                : this.octopus.passport.authorization.company,
            IsInventory: false
          },
          pageData: {}
        },
        data => {
          if (data.status.code == 200) {
            this.form.selectArr = data.content.queryData;
          } else {
            this.octopus.tips.error(data);
          }
        }
      );
    }
  }
};
</script>
<style scoped>
.drawer_box {
  padding: 3rem 0.8rem 3rem 0;
  height: calc(100% - 5rem);
  display: flex;
  flex-direction: column;
}

.drawer_title {
  box-shadow: 0px 5px 5px rgb(230 228 228 / 30%);
  padding-bottom: 1rem;
  font-size: 1.6rem;
}

.drawer_t {
  padding: 0 2.8rem;
  display: flex;
  justify-content: space-between;
}

.drawer_t_l {
  display: flex;
  align-items: center;
  font-size: 2rem;
  font-weight: bold;
}

.drawer_t_l_img {
  width: 2.8rem;
  margin-right: 1rem;
}

.drawer_t_r {
  width: 3rem;
  height: 3rem;
  cursor: pointer;
}

.button_box {
  margin-top: 1rem;
  padding: 0 3rem;
  display: flex;
}

.drawer_content {
  overflow-y: auto;
  margin-top: 1.5rem;
  height: calc(100vh - 11rem);
  padding: 0 3rem;
  width: 100%;
  padding-bottom: 1rem;
}

.drawer_content .title {
  font-size: 1.8rem;
  font-weight: bold;
}

.drawer_content .driver-title {
  margin-top: 1em;
}

.content-box {
  display: flex;
  align-items: center;
  margin: 2rem 0 2rem 2em;
}

.mt-10 {
  margin-top: 10rem;
}

.content-box .label {
  width: 25%;
  font-size: 1.6rem;
}

.content-box .label .label-bold {
  font-size: 1.8rem;
  font-weight: bold;
}

.content-box .desc {
  flex: 1;
  font-size: 1.6rem;
}

.list-box {
  display: flex;
  width: 100%;
  justify-content: space-between;
}

.list-box {
  line-height: 4rem;
  margin-top: 2rem;
}

.list-box .left {
  width: 20%;
  font-size: 1.5rem;
}

.list-box .right {
  width: 80%;
}

.button_list {
  margin-top: 1rem;
  display: flex;
  justify-content: end;
  position: absolute;
  background-color: #ffff;
  bottom: 1rem;
  width: calc(100% - 7rem);
}

.button_list button {
  border: none;
  height: 4rem;
  width: 12rem;
  border-radius: 1rem;
  margin-left: 1rem;
  color: #ffff;
  background-color: #f78c53;
  cursor: pointer;
}

.list-box .right .add_content {
  height: 4.5rem;
  line-height: 4.5rem;
  border: 1px dashed #f87338;
  display: inline-block;
  padding: 0 1.5rem;
  cursor: pointer;
  border-radius: 0.5rem;
}

.list-box .right .el-input {
  width: 100%;
  height: 4.5rem;
}

.list-box .right .add_content i {
  color: #f87338;
  font-weight: 700;
  margin-right: 0.5rem;
}
</style>
